var e = require("../../../utils/function.js"), t = getApp(), a = require("../../../api.js"), r = require("../../../qrcode.js");

Page({
    data: {
        title_txt:'请输入计费金额',
        charge_type:'',
        uploadSwitch: !1,
        backStatus: !1,
        isClick: !0,
        priceList: [ {
            num: 10
        }, {
            num: 20
        }, {
            num: 30
        } ],
        float_price: 0,
        dialog: !1,
        timer: null,
        showCanvas: !1,
        showForm: !1,
        total_price:'',
        img: e.driverImg,
        driver: {},
        order: {},
        is_cash: "",
        feeDetail: {
            speedFee: 0,
            roadToll: 2,
            otherFee: 3,
            order_no: ""
        }
    },
    onHide: function() {
        clearTimeout(this.timer);
    },
    carUpload: function(e) {
        var t = e.currentTarget.dataset.order_no;
        wx.navigateTo({
            url: "/pages/driver/carUpload/carUpload?order_no=" + t
        });
    },
    changeNum: function(e) {
        this.setData({
            float_price: Number(e.detail.value)
        });
    },
    showDialog: function() {
        this.setData({
            dialog: !0
        });
    },
    hideDialog: function() {
        this.setData({
            dialog: !1
        });
    },
    editBtnSubmit: function() {
        var e = this, r = this.data.order.order_no, o = this.data.float_price, p = this.data.total_price;
        t.request({
            url: a.order.editPrice,
            method: "post",
            data: {
                order_no: r,
                float_price: o,
                total_price: o,
            },
            success: function(t) {
                e.setData({
                    dialog: !1,
                    float_price: 0,
                    title_txt:this.data.total_price
                }), e.getFind(), wx.showToast({
                    title: t.msg,
                    icon: "none"
                });
            }
        });
    },
    changePrice: function(e) {
        var t = e.currentTarget.dataset.price;
        this.setData({
            float_price: Number(t)
        });
    },
    editBtn: function() {
        this.setData({
            dialog: !0
        });
    },
    getOrderStatus: function() {
        var e = this;
        t.request({
            url: a.order.get_order_status,
            method: "post",
            data: {
                order_no: e.data.order.order_no
            },
            success: function(t) {
                1 != t.is_pay ? (e.timer && clearTimeout(e.timer), e.timer = setTimeout(function() {
                    e.getOrderStatus();
                }, 5e3)) : (clearTimeout(e.timer), wx.showToast({
                    title: "支付成功"
                }), setTimeout(function() {
                    wx.navigateBack({
                        delta: 1
                    });
                }, 800));
            }
        });
    },
    hideCanvas: function() {
        this.setData({
            showCanvas: !1
        });
    },
    showCanvas: function() {
        this.setData({
            showCanvas: !0
        });
    },
    getQrCode: function(e) {
        this.setData({
            showCanvas: !0
        });
        new r("canvas", {
            text: e,
            width: 200,
            height: 200,
            colorDark: "#000000",
            colorLight: "#ffffff",
            correctLevel: r.CorrectLevel.H
        });
    },
    changeValue: function(e) {
        var t = e.currentTarget.dataset.key, a = e.detail.value, r = {};
        r[t] = a, this.setData(r);
    },
    closeForm: function() {
        this.setData({
            showForm: !1
        });
    },
    orderHandle: function(e) {
        var t = e.currentTarget.dataset.order_no;
        this.setData({
            showForm: !0,
            order_no: t
        });
    },
    handleConfirm: function() {
        var e = this, r = e.data, o = r.order_no, n = r.actualKm, s = r.waitTime;
        t.request({
            url: a.order.order_handle,
            method: "post",
            data: {
                order_no: o,
                actual_km: n,
                wait_time: s
            },
            success: function(t) {
                e.getFind(), e.isCash(), e.setData({
                    showForm: !1
                }), wx.showToast({
                    title: t.msg,
                    icon: "none"
                });
            }
        });
    },
    skipPageHandler: function(e) {
        var t = e.currentTarget.dataset.order_no;
        wx.navigateTo({
            url: "/pages/userOrder/orderTrack/orderTrack?order_no=" + t
        });
    },
    selectPhoto: function(e) {
        var t = e.currentTarget.dataset.order_no;
        wx.navigateTo({
            url: "/pages/driverOrder/selectPhoto/index?order_no=" + t
        });
    },
    intoPageHandler: function(e) {
   if (this.data.charge_type == 2)  {
    if (this.data.title_txt == '请输入计费金额' || this.data.title_txt == !1 ) {
        wx.showToast({
        title: '请输入金额',
        icon: 'none',
        })
      return false
   }
   } 
    //    if (this.data.title_txt == '请输入计费金额' || this.data.title_txt == !1 ) {
    //         wx.showToast({
    //         title: '请输入金额',
    //         icon: 'none',
    //         })
    //       return false
    //    }
      
        var r = this;
        r.data.backStatus = !0;
        var o = e.currentTarget.dataset.type, n = e.detail.formId, s = r.data.order;
        if (3 == o || 4 == o || 5 == o || 1 == s.type && 0 == s.paid) return this.showQrCode(e.currentTarget.dataset.order_no, r.data.order.actual_price), 
        !1;
        t.request({
            url: a.order.initiate,
            method: "post",
            data: {
                order_no: e.currentTarget.dataset.order_no,
                actual_price: e.currentTarget.dataset.paid,
                formid: n
            },
            success: function(a) {
                var o = this;
                1 == a.code ? (s.uid != s.driverid && t.gateway(e.currentTarget.dataset.order_no, 3), 
                r.closeAlert(function() {
                    wx.redirectTo({
                        url: e.currentTarget.dataset.skip + "?paid=" + e.currentTarget.dataset.paid + "&speedFee=" + e.currentTarget.dataset.speedfee + "&roadToll=" + e.currentTarget.dataset.roadtoll + "&otherFee=" + e.currentTarget.dataset.otherfee + "&order_no=" + o.data.order_no
                    });
                })) : wx.showToast({
                    title: "你已催款，请稍后",
                    duration: 1e3,
                    icon: "none"
                });
            }
        });
    },
    showQrCode: function(e, r) {
        var o = this;
        t.request({
            url: a.order.prepay,
            method: "post",
            data: {
                order_no: e,
                pay_type: 4,
                pay_price: 1,
               
                // pay_price: r,
                pay: "3"
            },
            success: function(e) {
                1 == e.code ? (o.getQrCode(e.data.code_url), setTimeout(function() {
                    o.getOrderStatus();
                }, 5e3)) : wx.showToast({
                    title: e.msg,
                    icon: "none"
                });
            }
        });
    },
    callPhoneHandler: function(t) {
        e.callPhoneHandler(t.currentTarget.dataset.phone);
    },
    addAdnReduceHandler: function(e) {
        var t = e.currentTarget.dataset.types, a = e.currentTarget.dataset.params, r = this.data.feeDetail;
        "add" === t ? (r[a]++, this.setData({
            feeDetail: r
        })) : r[a] > 0 && (r[a]--, this.setData({
            feeDetail: r
        }));
    },
    getCash: function(e) {
        var r = this, o = r.data.order;
        if (r.data.backStatus = !0, !r.data.isClick) return wx.showToast({
            title: "请勿重复点击",
            icon: "none"
        }), !1;
        r.data.isClick = !1, t.request({
            url: a.order.cash,
            method: "post",
            data: {
                order_no: r.data.order_no,
                price: this.data.total_price
            },
            success: function(e) {
                r.data.isClick = !0, 1 == e.code && (o.driverid != o.uid && t.gateway(r.data.order_no, 4), 
                r.closeAlert(function() {
                    // wx.navigateBack({
                    //     delta: 1
                    // });
                    wx.navigateTo({
                      url: '../code/index',
                    })
                })), wx.showToast({
                    title: e.msg,
                    icon: "none"
                });
            }
        });
    },
    onLoad: function(e) {
        wx.canIUse("enableAlertBeforeUnload") ? wx.enableAlertBeforeUnload({
            message: "未选择支付方式，是否退出?"
        }) : wx.showModal({
            title: "提示",
            content: "微信版本过低，导致某些功能无法使用，请尽快升级",
            showCancel: !1
        }), this.data.backStatus && this.closeAlert(function() {}), this.setData({
            order_no: e.order_no,
            uploadSwitch: wx.getStorageSync("OPEN_CAR_INFO")
        }), e.uid && t.shareCouponsMessage(e.uid), this.isCash();
    },
    judge: function() {},
    isCash: function() {
        var e = this;
        t.request({
            url: a.order.is_cash,
            method: "post",
            data: {},
            success: function(t) {
                e.setData({
                    is_cash: t.data.is_cash
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {
        this.getFind();
    },
    onUnload: function() {},
    closeAlert: function(e) {
        wx.canIUse("disableAlertBeforeUnload") && (wx.disableAlertBeforeUnload(), e());
    },
    getFind: function() {
        var e = this;
        console.log(a.order.getfind)
        wx.showLoading({}), t.request({
            url: a.order.getfind,
            method: "post",
            data: {
                order_no: e.data.order_no
            },
            success: function(t) {
                console.log(t);
                console.log(t.data.charge_type);
                wx.hideLoading(), 1 == t.code ? (t.data.actual_price = Number(t.data.actual_price), 
                t.data.float_price = Number(t.data.float_price), 1 == t.data.is_pay && e.closeAlert(function() {}), 
                e.setData({
                    order: t.data,
                    total_price:t.data.total_price,
                    charge_type:t.data.charge_type
                }), wx.getStorageSync("server_phone", t.data.passenger_phone)) : (wx.showToast({
                    title: t.msg
                }), wx.redirectTo({
                    url: "../orderGoing/orderGoing?order_no=" + e.data.order_no
                }));
            }
        });
    },
    formSubmit: function(t) {
        var a = wx.getStorageSync("user_info").id, r = t.detail.formId;
        if (a && r) {
            var o = {
                form_id: r,
                user_id: a
            }, n = this;
            "true" == t.currentTarget.dataset.istrue ? 1 == wx.getStorageSync("user_info").user_type ? this.login_test("../teacher-appointment-record/index") : this.login_test("../user-appointment-record/index") : (e.requestData("storageFormid", o, function(e) {}), 
            n.handler_next_page(t));
        }
    },
    onShareAppMessage: function() {
        return {
            title: t.shareAppMessage().title,
            imageUrl: t.shareAppMessage().imageUrl,
            desc: t.shareAppMessage().desc,
            path: t.shareAppMessage().path
        };
    }
});